import { createRouter, createWebHistory } from 'vue-router';
import { staticRoutes } from '@/router/routes';
import { useUserInfoStore } from '@/stores/userInfo';

import pinia from '@/stores';
const userInfoStore = useUserInfoStore(pinia);
const router = createRouter({
  history: createWebHistory(),
  routes: staticRoutes,
  scrollBehavior() {
    return { top: 0, left: 0 }
  },
})
// 定义黑名单
let blackList = ['/user']
// 路由前置守卫
router.beforeEach((to,from,next) => {
  // 判断将要跳转的路由是不是在黑名单中，如果不在直接跳转，如果在判断是否登录
  if (!blackList.includes(to.fullPath)) {
    next()
  }else{
    // 获取token
    const userStore = useUserInfoStore()
    let token = userStore.token
    // 如果有token直接跳
    if (token) {
      next()
    }else{
      // 没有token跳转到登录页并记录要去的页面
      next('/login' + `?redirect=${to.fullPath}`)
    }
  }
})
// 导出路由
export default router;